#include "include/gudb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int test_database_isolation() {
    printf("=== Testing Core Database Isolation Logic ===\n");
    
    // Open database
    gudb* db = gudb_open("/tmp/gudb_data", 0, "admin", "admin");
    if (!db) {
        printf("❌ Failed to open database\n");
        return -1;
    }
    
    printf("✅ Database opened successfully\n");
    
    // Test 1: Create transaction with default database
    printf("\n--- Test 1: Default database transaction ---\n");
    gudb_txn* txn1 = gudb_begin(db, GU_TXN_RC, "gudb");
    if (!txn1) {
        printf("❌ Failed to create transaction\n");
        gudb_close(db);
        return -1;
    }
    
    // Check current database
    const char* current_db1 = gudb_txn_current_database(txn1);
    printf("Transaction 1 current database: %s\n", current_db1 ? current_db1 : "NULL");
    
    // Execute SHOW TABLES
    result_t* result1 = NULL;
    int ret1 = gudb_exec(txn1, "SHOW TABLES", NULL, &result1);
    printf("SHOW TABLES result: %d\n", ret1);
    if (result1 && result1->col_count > 0) {
        printf("Column header: %s\n", result1->column_names[0]);
        printf("Row count: %u\n", result1->row_count);
    }
    
    // Clean up
    if (result1) gudb_free_result(result1);
    gudb_rollback(txn1);
    
    // Test 2: Create transaction with testdb2 database
    printf("\n--- Test 2: testdb2 database transaction ---\n");
    gudb_txn* txn2 = gudb_begin(db, GU_TXN_RC, "testdb2");
    if (!txn2) {
        printf("❌ Failed to create transaction with testdb2\n");
        gudb_close(db);
        return -1;
    }
    
    // Check current database
    const char* current_db2 = gudb_txn_current_database(txn2);
    printf("Transaction 2 current database: %s\n", current_db2 ? current_db2 : "NULL");
    
    // Execute SHOW TABLES
    result_t* result2 = NULL;
    int ret2 = gudb_exec(txn2, "SHOW TABLES", NULL, &result2);
    printf("SHOW TABLES result: %d\n", ret2);
    if (result2 && result2->col_count > 0) {
        printf("Column header: %s\n", result2->column_names[0]);
        printf("Row count: %u\n", result2->row_count);
    }
    
    // Clean up
    if (result2) gudb_free_result(result2);
    gudb_rollback(txn2);
    
    // Test 3: Test USE command within transaction
    printf("\n--- Test 3: USE command within transaction ---\n");
    gudb_txn* txn3 = gudb_begin(db, GU_TXN_RC, "gudb");
    if (!txn3) {
        printf("❌ Failed to create transaction\n");
        gudb_close(db);
        return -1;
    }
    
    printf("Before USE - current database: %s\n", 
           gudb_txn_current_database(txn3) ? gudb_txn_current_database(txn3) : "NULL");
    
    // Execute USE command
    result_t* use_result = NULL;
    int use_ret = gudb_exec(txn3, "USE testdb2", NULL, &use_result);
    printf("USE testdb2 result: %d\n", use_ret);
    if (use_result) gudb_free_result(use_result);
    
    printf("After USE - current database: %s\n", 
           gudb_txn_current_database(txn3) ? gudb_txn_current_database(txn3) : "NULL");
    
    // Execute SHOW TABLES after USE
    result_t* result3 = NULL;
    int ret3 = gudb_exec(txn3, "SHOW TABLES", NULL, &result3);
    printf("SHOW TABLES after USE result: %d\n", ret3);
    if (result3 && result3->col_count > 0) {
        printf("Column header: %s\n", result3->column_names[0]);
        printf("Row count: %u\n", result3->row_count);
    }
    
    // Clean up
    if (result3) gudb_free_result(result3);
    gudb_rollback(txn3);
    
    gudb_close(db);
    printf("\n=== Core Database Isolation Test Complete ===\n");
    return 0;
}

int main() {
    return test_database_isolation();
}